/**

 * @author admin

 * 2014年7月15日

 */

package com.tmall.testgen.plugin.views.vo;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.tmall.testgen.client.runinspring.RunInSpringUtil;
import com.tmall.testgen.common.util.Object2TreeMap;
import com.tmall.testgen.common.util.bo.FieldClassAndValue;

public class SpringTree  extends TreeParent {

	private Class clazz;
	private String fieldName;
	
	
	
	public void initName(){
		if(getClazz()==null){
			name="null";
			return;
		}
		name=getClazz().getSimpleName()+" "+getFieldName();
		if(clazz!=null&&value!=null&&RunInSpringUtil.isBasicType(clazz)){
		    if(value instanceof Date){
		        DateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		        name+="="+sf2.format(value);  
		    }else{
		        name+="="+value;  
		    }
		
		}
	}

	/**
	 * 解析子类的name并加入树中，仅name
	 */
	@SuppressWarnings("rawtypes")
	public void addFiledName(){
		if(isHandled)
			return;
		
		isHandled=true;
		
		this.setLeafNode(false);
			if(value!=null){
				RunInSpringUtil.addValue2Tree(this, value);
			}else{
				try{
					value=clazz.newInstance();
				}catch(Exception e){
					
				}
				Map<String,FieldClassAndValue> fieldMap=Object2TreeMap.getFieldMapFromClass(clazz,value,this.isEditable());
				
				for(String fieldName:fieldMap.keySet()){
					SpringTree node = new SpringTree();
					
					node.setClazz(fieldMap.get(fieldName).getClazz());
					
					node.fieldName=fieldName;
					
					try{
					node.setValue(fieldMap.get(fieldName).getValue());
					}catch(Exception e){
					}
					node.setLeafNode(node.getValue()==null||RunInSpringUtil.isBasicType(node.getClazz()));
					node.initName();
					this.addChild(node);
				}
			}
	}


	public Class getClazz() {
		return clazz;
	}


	public void setClazz(Class clazz) {
		this.clazz=Object2TreeMap.getClass(clazz);
	}


	public String getFieldName() {
		return fieldName;
	}


	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}
	

}
